package stu.day25_day32.day31;

public class hw1 {

    public boolean flag = false;
    public boolean exist(char[][] board, String word) {
        int n = board.length;
        int m = board[0].length;
        int[][] index = new int[n][m];
        for(int i=0;i<n;i++){
            for (int j=0;j<m;j++){

                words(i,j,0,board,word,index);
            }
        }
        return  flag;

    }

    public void words(int i,int j,int len,char[][] board, String word,int[][] index){

        if(len==word.length()){
            flag = true;
            return;
        }

        if(i>=0 && i<board.length && j>=0 && j<board[0].length){
            if(board[i][j]==word.charAt(len) && index[i][j]==0){
                index[i][j] = 1;
                words(i+1,j,len+1,board,word,index);
                words(i-1,j,len+1,board,word,index);
                words(i,j+1,len+1,board,word,index);
                words(i,j-1,len+1,board,word,index);
                index[i][j] = 0;
            }
        }
    }



}
